from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import json



def open_url(url):
    browser.get(url)
    # print(browser.page_source)
    time.sleep(3)
    input_auth()
    
def input_auth():
    phoneNumber = browser.find_element(By.CSS_SELECTOR,'input[id="phoneNumber"]')
    phoneNumber.clear()
    phoneNumber.send_keys("15021039558")
    password = browser.find_element(By.CSS_SELECTOR,'input[id="password"]')
    password.clear()
    password.send_keys("NXc@2305")

def load_tree_info():
  with open("res/tree.json",'r') as fd:
    tree_info = json.loads(fd.readlines)
  with open("res/cur_tree_info.json",'r') as fd:
    cur_tree_info = json.loads(fd.readlines)
  return tree_info,cur_tree_info

def save_tree_info(tree_info, cur_tree_info):
  with open("res/tree.json",'w') as fd:
    fd.write(json.dumps(tree_info))
  with open("res/cur_tree_info.json",'w') as fd:
    fd.write(json.dumps(cur_tree_info))

def click_tree():
  windedb_title_div = browser.find_element(By.CSS_SELECTOR,"div.async-display-tree-node div.name-content[title='WindEDB']")
  dfs_click(windedb_title_div)

def tree_list(level):
  windedb_class = f"div[class='async-display-tree-node level-{level}']"
  span_list = browser.find_elements(By.CSS_SELECTOR,f"{windedb_class}>span")
  print("span_list_len",len(span_list))

  for i in range(len(span_list)):
    # if i<908:
    #   continue
    # if i> 1000:
    #   break
    span_list[i].click()
    time.sleep(3)
    text_content = span_list[i].get_attribute("textContent").strip()
    print(text_content)
   
def use_chrome():
  #先启动调试浏览器
  #"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
  option=webdriver.ChromeOptions()
  option.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
  browser=webdriver.Chrome(options=option)
  return browser

def use_edge():
  from selenium.webdriver.edge.service import Service
  s=Service(r"C:\Users\37048\Downloads\edgedriver_win64\msedgedriver.exe")#驱动路径方式如上
  driver=webdriver.Edge(service=s)
  driver.maximize_window()#最大化窗口
  driver.get("http://www.baidu.com")

def dfs_click(cur):
  print(type(cur))
  print()
  # cur.click()
  time.sleep(3)
  child_div = browser.find_elements(By.CSS_SELECTOR,"div.async-display-tree-node div.name-content[title='WindEDB']")
  print(child_div,len(child_div))
  # for child in child_div:
  #   dfs_click(child)
  
if __name__ == '__main__':
  #需要把Chromedriver加入到path，下载地址
  browser = use_chrome()
  # use_edge()
  # open_url("http://rbdacs.ccnp.cib/#/login")
  # input_auth()
  # click_tree()
  level_list = list(range(8)[3:])
  for i in level_list:
    print(i)
    tree_list(i)